// 该文件用于创建vuex中最为核心的store

// 引入Vue
import Vue from "vue";
//应用 Vuex 插件
import Vuex from "vuex";
//使用vuex插件
Vue.use(Vuex);
import {
  sendFeedback,
  askEchartsInfo,
  login,
  historySimulation,
  getHistoryEvaluate,
  getInfoMyslef,
} from "../api";

// 准备actions：用于响应组件中的动作
const actions = {
  async prepareSendFeedback(context, value) {
    // 发送请求
    let res = await sendFeedback(value);
    if (res.status == 200) {
      context.commit("GETFEEDBACK", res.data);
    }
  },
  async askForEchartsInfo(context) {
    let res = await askEchartsInfo();
    if (res.status == 200) {
      console.log(context);
    }
  },
  async loginTest(context, value) {
    let res = await login(value[0]);
    if (res && res.status == 200) {
      context.commit("STOREUSERNAME", res.data);
      value[1].replace("/home");
    }
  },
  async getHistory(context, value) {
    if (!context.state.username[0]) {
      alert("请先登录！O(∩_∩)O");
      value.replace("/login");
      return;
    }
    let res = await historySimulation(context.state.username[0]);
    if (res && res.status == 200) {
      context.commit("GETHISTORY", res.data);
    }
  },
  async initailizeEvaluate(context) {
    let res = await getHistoryEvaluate();
    if (res && res.status == 200) {
      context.commit("GETHISTORYEVALUATE", res.data);
    }
  },
  async getInfoAboutMe(context, value) {
    if (!context.state.username[0]) {
      alert("请先登录！O(∩_∩)O");
      value.replace("/login");
      return;
    }
    let res = await getInfoMyslef(context.state.username[0]);
    if (res.status == 200) {
      context.commit("GETINFOABOUTME", res.data);
    }
  },
};
// 准备mutations—用于操作数据（state）
const mutations = {
  GETFEEDBACK(state, value) {
    state.feedbackList = value;
  },
  STOREECHATRTS(state, value) {
    state.echartsInfo = value;
  },
  STOREUSERNAME(state, value) {
    state.username = value;
  },
  GETHISTORY(state, value) {
    state.historyRecords = value;
    console.log(state.histotyRecords);
  },
  GETHISTORYEVALUATE(state, value) {
    state.feedbackList = value;
  },
  GETINFOABOUTME(state, value) {
    console.log(value);
    state.info = value;
  },
};
//准备state—用于存储数据
const state = {
  feedbackList: [],
  echartsInfo: [],
  username: [],
  historyRecords: [],
  info: {},
};

// 创建并暴露store
export default new Vuex.Store({
  actions,
  mutations,
  state,
});
